﻿import os
from detecter.config import *
# import netifaces

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

SECRET_KEY = 'aty_+9+hfpf0=f@06cx(_sg!ecffb6$n#!#y+k1c1_buu&e!g+'

CONTENT_TYPE = "application/json;charset=utf-8"
DEBUG = False
IS_TEST = False

###################################
# 通过ip地址获取对应的机台号
IP_CODE = {'192.168.5.101': 'A01', '192.168.5.102': 'A02', '192.168.5.103': 'A03', '192.168.5.104': 'A04', '192.168.5.105': 'A05',
           '192.168.5.106': 'A06', '192.168.5.107': 'A07', '192.168.5.108': 'A08', '192.168.5.109': 'A11', '192.168.5.110': 'A12',
           '192.168.5.111': 'B05', '192.168.5.112': 'B06', '192.168.5.113': 'B07', '192.168.5.114': 'B08', '192.168.5.115': 'B09',
           '192.168.5.116': 'C02', '192.168.5.117': 'C03', '192.168.5.118': 'C04', '192.168.5.119': 'C05', '192.168.5.120': 'C06',
           '192.168.5.121': 'C07',
           '192.168.5.122': 'D05', '192.168.5.123': 'D06', '192.168.5.124': 'D07', '192.168.5.125': 'D08', '192.168.5.126': 'D09',
           '192.168.5.127': 'D10', '192.168.5.128': 'D11', '192.168.5.129': 'D12', '192.168.5.130': 'D13',
           '192.168.5.131': 'E01', '192.168.5.132': 'E02', '192.168.5.133': 'E04', '192.168.5.134': 'E05', '192.168.5.135': 'E06',
           '192.168.5.139': 'A14', '192.168.5.138': 'A13', '192.168.5.137': 'A10', '192.168.5.136': 'A09',
           '192.168.1.58': "A00", '192.168.0.58': "D07"
           }

# IP_CODE = {}
###################################
# 获取本机ip


MACHINE_CODE = get_ip_address(["enp2s0","enp4s0"],IP_CODE)

frame_rate = 50



# SERVER_URL = 'http://192.168.1.92:81/'   # 新的平板网页界面地址
# SERVER_URL = 'http://192.168.1.92:8080/hd-api'
# SERVER_URL = 'http://192.168.6.92:81/hd-api'
SERVER_URL = 'http://192.168.1.92:81/hd-api'
TEST_SERVER_URL = 'http://192.168.1.92:8080/hd-api'
NEG_FIGURE_UPDATE_FREQ = 10  # 负样本画像更新频率 10个/次
LATEST_VAR_LENGTH = 72  # 过去72小时的制程稳定性
MIN_SAMPLES_TO_CLUSTER = -999  # 进行负样本聚类的最小数量，用于前端提示
NEGATIVE_LABEL_NUMBER = 11  # 目前的异常现象总数
NEGATIVE_REASON_NUMBER = 10  # 目前的异常原因总数

Collection_frequency = 1  # 10Hz相关采集频率配置

# zxy 修改于 20200104
DEFAULT_THRESHOLD = 5.0  # 默认总体判断阈值
POSITIVE_START = 50  # 第一次构建正样本画像时所需的正样本数量
POSITIVE_UPDATE = 50  # 每隔多少正样本进行画像更新;
NEGATIVE_START = 5  # 第一次构建负样本画像时所需的正样本数量
NEGATIVE_UPDATE = 5  # 每隔多少负样本进行画像更新

#####################聚类算法参数##############
DBSCAN_MINPts = 5  # dbscan算法中的MinPts
DBSCAN_EPS_RATIO = 0.4  # 取值为0.1-0.9，数值越小聚类越分散，越大聚类紧凑但是会有跟多离散点
PRANK_LAMDA = 0.8  # P-Rank算法中的lamda(结构中In的权重，Out即为1-lamda)
PRANK_C = 0.5  # P-Rank算法中的C(每次迭代的衰减率)
PRANK_K = 5  # P-Rank算法中的k(迭代次数)

MAX_CLUSTER_SAMPLES = 100  # 运行聚类算法时读取最近的样本数量，若为100，则从文件中读取最近的100个负样本
E2HASH_K = 20  # e2Hash算法中fpRand的维度
E2HASH_MAX_N = 500  # e2Hash算法中最多处理的样本特征维度
E2HASH_BUCKET_NUMBER = 100  # 哈希桶中预计的样本数量
#################################################


# 串口服务器设置
producer_res = '192.168.4.58'
producer_port = 8898

###############串口设置###############################
ip = "192.168.4.124"
port1 = 51501
port2 = 52501
com = '/dev/ttyS0'
baud = 115200

PORT_PRODUCT = '/dev/ttyS0'
PORT_PRODUCT_set = '/dev/ttyS1'
BAUD_RATE_PRODUCT = 115200
PORT_SEND = '/dev/ttyS2'
BAUD_RATE_SEND = 9600
RESET_TIME_SEND = 15  # 发送复位信号与分拣信号的间隔，单位秒

PORT_LISTEN = '/dev/ttyS1'
BAUD_RATE_LISTEN = 115200
#####################################################

ALLOWED_HOSTS = ['*']
EXAMPLE_PATH = os.path.join(BASE_DIR, 'data', 'example')

WEBSOCKET_ACCEPT_ALL = True
DATE_FORMAT = '%Y-%m-%d %H:%M:%S'

DATA_BITS = 8
PARITY = '无'
STOP_BITS = 1
PRESSURE_FRAME_SIZE = 74  # 监听模式 监听8路模拟量指定字节数数据
TEMP_FRAME_SIZE = 15
LABEL_FRAME_SIZE = 8  # listener线程采用的通信协议的一个周期的数据帧长度
VOLTAGE_FRAME_SIZE = 9  # listener线程采集电压帧的长度

INSTALLED_APPS = [
    'dwebsocket',
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'api',#api.apps.ApiConfig
    'corsheaders',
]

MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    # 'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
    'corsheaders.middleware.CorsMiddleware',
    'django.middleware.common.CommonMiddleware',
]

ROOT_URLCONF = 'detecter.urls'

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [os.path.join(BASE_DIR, 'templates')]
        ,
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]

WSGI_APPLICATION = 'detecter.wsgi.application'

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
        'OPTIONS': {
            'timeout': 20,
        },
    }
}

AUTH_PASSWORD_VALIDATORS = [
    {
        'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
    },
]

# Internationalization
# https://docs.djangoproject.com/en/2.2/topics/i18n/

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'Asia/Shanghai'

USE_I18N = True

USE_L10N = True

USE_TZ = False

STATIC_URL = '/static/'

LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'formatters': {
        'standard': {
            'format': '[%(levelname)s][%(asctime)s][%(filename)s][%(funcName)s][%(lineno)d] > %(message)s'
        },
        'simple': {
            'format': '[%(levelname)s]> %(message)s'
        },
    },
    'filters': {
    },
    'handlers': {
        'default': {
            'level': 'WARNING',
            'class': 'logging.handlers.RotatingFileHandler',
            'filename': os.path.join('logs', 'default.log'),
            'maxBytes': 1024 * 1024 * 30,
            'backupCount': 100,
            'formatter': 'standard',
            'encoding': 'utf-8',
        },
        'console': {
            'level': 'INFO',
            'class': 'logging.StreamHandler',
            'formatter': 'standard'
        },
        'file': {
            'level': 'INFO',
            # 'class': 'logging.handlers.TimedRotatingFileHandler',
            'class': 'detecter.rewriting_TimedRotatingFileHandler.CommonTimedRotatingFileHandler',
            'filename': os.path.join('logs', 'history.log'),
            # 保留60份日志
            'when': 'midnight',  # 间间隔的类型
            'interval': 1,  # 时间间隔
            'backupCount': 100,  # 能留几个日志文件;过数量就会丢弃掉老的日志文件
            'formatter': 'standard',  # 日志文本格式
            'encoding': 'utf-8',  # 日志文本编码
        },
    },
    'loggers': {
        'django': {
            'handlers': ['default', 'console', 'file'],
            'level': 'DEBUG',
            'propagate': True
        },
        'serial.sample': {
            'handlers': ['console', 'file'],
            'level': 'DEBUG',
            'propagate': False
        },
        'serial.producer': {
            'handlers': ['console', 'file'],
            'level': 'DEBUG',
            'propagate': False
        },
        'pojie_A10.producer_actual_values_A10': {
            'handlers': ['console', 'file'],
            'level': 'DEBUG',
            'propagate': False
        },
        'pojie_A10.producer_actual_values_yqd_A10': {
            'handlers': ['console', 'file'],
            'level': 'DEBUG',
            'propagate': False
        },
        'pojie_A10.producer_set_values_A10': {
            'handlers': ['console', 'file'],
            'level': 'DEBUG',
            'propagate': False
        },
        'pojie_A10.computing_method_A10': {
            'handlers': ['console', 'file'],
            'level': 'DEBUG',
            'propagate': False
        },
        'serial.order': {
            'handlers': ['console', 'file'],
            'level': 'DEBUG',
            'propagate': False
        },
        'serial.consumer': {
            'handlers': ['console', 'file'],
            'level': 'DEBUG',
            'propagate': False
        },
        'serial.listener': {
            'handlers': ['console', 'file'],
            'level': 'DEBUG',
            'propagate': False
        },
        'webRequest.feedback': {
            'handlers': ['console', 'file'],
            'level': 'DEBUG',
            'propagate': False
        },
        'webRequest.assign': {
            'handlers': ['console', 'file'],
            'level': 'DEBUG',
            'propagate': False
        }
    }
}
# 跨域增加忽略
CORS_ALLOW_CREDENTIALS = True
CORS_ORIGIN_ALLOW_ALL = True
CORS_ORIGIN_WHITELIST = ()

CORS_ALLOW_METHODS = (
    'DELETE',
    'GET',
    'OPTIONS',
    'PATCH',
    'POST',
    'PUT',
    'VIEW',
)

CORS_ALLOW_HEADERS = (
    'accept',
    'accept-encoding',
    'authorization',
    'content-type',
    'dnt',
    'origin',
    'user-agent',
    'x-csrftoken',
    'x-requested-with',
)
